﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.DXErrorProvider;


namespace QuanLyThuNgan
{
    public partial class Frm_DM_LyDoKhongThuTien : Form
    {
        public Frm_DM_LyDoKhongThuTien()
        {
            InitializeComponent();
        }

        string sTenSearch = "";
        string sColSearch;
        string sColRow;

        DMLyDoKhongThuTienCTL lyDoKTTCtl = new DMLyDoKhongThuTienCTL();
        DMLyDoKhongThuTienDTO lyDoKTTDto = new DMLyDoKhongThuTienDTO();

        private void Frm_DM_LyDoKhongThuTien_Load(object sender, EventArgs e)
        {
            DataTable dtktt = lyDoKTTCtl.m_SelectDMLyDoKTT();
            grdDMLyDoKTT.DataSource = dtktt;

            //set condition load form----------------------------
            gvDMLyDoKTT.OptionsBehavior.ReadOnly = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = false;
            gvDMLyDoKTT.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)grdDMLyDoKTT.DataSource;
            if (dt != null)
            {
                for (int i = 0; i < gvDMLyDoKTT.RowCount; i++)
                {
                    DataRow dr = gvDMLyDoKTT.GetDataRow(i);
                    gvDMLyDoKTT.MoveNext();
                    gvDMLyDoKTT.MovePrev();
                    if (dr["TenLyDoKhongThuTien"].ToString() != "")
                    {
                        lyDoKTTDto.STenLyDo = dr["TenLyDoKhongThuTien"].ToString().Trim();
                        lyDoKTTDto.BStop = Uit.it_Parse.ToBoolean(dr["bStop"].ToString());
                        if (dr["IDLyDoKhongThuTien"].ToString() == "")
                        {
                            lyDoKTTCtl.m_InsertDMLyDoKTT(lyDoKTTDto);
                        }
                        else
                        {
                            lyDoKTTDto.IIDLyDoKTT = Uit.it_Parse.ToInteger(dr["IDLyDoKhongThuTien"].ToString());
                            lyDoKTTCtl.m_UpdateDMLyDoKTT(lyDoKTTDto);
                        }
                    }

                }
                XtraMessageBox.Show("Lưu thành công");

            }
            DataTable dtkttload = lyDoKTTCtl.m_SelectDMLyDoKTT();
            grdDMLyDoKTT.DataSource = dtkttload;

            btnSave.Enabled = false;
            btnUpdate.Enabled = true;
            btnInsert.Enabled = true;
            gvDMLyDoKTT.OptionsBehavior.ReadOnly = true;
            gvDMLyDoKTT.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataRow dr = gvDMLyDoKTT.GetDataRow(gvDMLyDoKTT.FocusedRowHandle);
            if (dr != null)
            {
                lyDoKTTDto.IIDLyDoKTT = Uit.it_Parse.ToInteger(dr["IDLyDoKhongThuTien"].ToString());
                lyDoKTTCtl.m_DeleteDMLyDoKTT(lyDoKTTDto);
            }
            XtraMessageBox.Show("Xóa thành công");
            DataTable dtkttload = lyDoKTTCtl.m_SelectDMLyDoKTT();
            grdDMLyDoKTT.DataSource = dtkttload;
            //reset load du lieu----------------------
            gvDMLyDoKTT.ActiveFilterEnabled = false;
            gvDMLyDoKTT.ClearColumnsFilter();
            gvDMLyDoKTT.OptionsFilter.Reset();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            gvDMLyDoKTT.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = false;
            btnInsert.Enabled = false;
            gvDMLyDoKTT.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            DataTable dtkttload = lyDoKTTCtl.m_SelectDMLyDoKTT();
            grdDMLyDoKTT.DataSource = dtkttload;
            //reset load du lieu----------------------
            gvDMLyDoKTT.ActiveFilterEnabled = false;
            gvDMLyDoKTT.ClearColumnsFilter();
            gvDMLyDoKTT.OptionsFilter.Reset();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            gvDMLyDoKTT.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnInsert.Enabled = true;
            btnSave.Enabled = false;
            gvDMLyDoKTT.OptionsBehavior.ReadOnly = true;
            DataTable dtkttload = lyDoKTTCtl.m_SelectDMLyDoKTT();
            grdDMLyDoKTT.DataSource = dtkttload;
            gvDMLyDoKTT.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            //reset load du lieu----------------------
            gvDMLyDoKTT.ActiveFilterEnabled = false;
            gvDMLyDoKTT.ClearColumnsFilter();
            gvDMLyDoKTT.OptionsFilter.Reset();
        }

        private void gvDMLyDoKTT_ValidateRow(object sender, ValidateRowEventArgs e)
        {
            GridView view = gvDMLyDoKTT;
            GridColumn colLyDo = view.Columns["TenLyDoKhongThuTien"];

            //get value in row---------------------------
            int iRowIndex = e.RowHandle;
            //if (sColRow != "bNgungSuDung")
            //{
            string sTenLyDo = view.GetRowCellValue(e.RowHandle, colLyDo).ToString().ToUpper().Trim();
            if (sTenLyDo == "")
            {
                e.Valid = false;
                view.SetColumnError(colLyDo, "Tên lý do không được trống");
            }

            //set duyet gia tri trong vong lap for 0-n
            for (int i = 0; i < gvDMLyDoKTT.RowCount; i++)
            {
                if (i != iRowIndex)
                {
                    DataRow drLyDo = gvDMLyDoKTT.GetDataRow(i);
                    string test = drLyDo["TenLyDoKhongThuTien"].ToString().ToUpper().Trim();
                    //so sanh gia tri can duyet voi gia tri trong vong lap---
                    if (sTenLyDo == test)
                    {
                        e.Valid = false;
                        XtraMessageBox.Show("Tên lý do không được trùng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        view.SetColumnError(colLyDo, "Tên lý do không được trùng");
                        return;
                    }
                }
            }

            //}
            btnSave.Enabled = true;
        }

        private void gvDMLyDoKTT_FocusedColumnChanged(object sender, FocusedColumnChangedEventArgs e)
        {
            sColRow = e.FocusedColumn.FieldName;
        }

        private void gvDMLyDoKTT_CellValueChanging(object sender, CellValueChangedEventArgs e)
        {
            sTenSearch = e.Value.ToString();
            sColSearch = e.Column.FieldName;
        }

        private void gvDMLyDoKTT_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gvDMLyDoKTT;
            view.ActiveFilter.Add(view.Columns[sColSearch], new ColumnFilterInfo("[" + sColSearch + "] Like '%" + sTenSearch + "%'", ""));
        
        }


    }
}
